package com.xinxin.facelinker.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.adapter.GridViewMotionPhotoAdapter;
import com.xinxin.facelinker.adapter.ShowMotionAdapter;
import com.xinxin.facelinker.domain.ShowMotion;
import com.xinxin.facelinker.domain.ShowMyMotion;
import com.xinxin.facelinker.domain.ShowPalsMotion;
import com.xinxin.facelinker.utils.NetHelper;

import java.util.ArrayList;
import java.util.List;

public class ShowMotionActivity extends Activity {

    private String motion_id;
    private String[] motion_photo_url;
    private String photo_url;
    private String nickname;
    private String date;
    private String motion;
    private HttpUtils httpUtils = new HttpUtils();
    private View btnBack;
    private ListView lvShowMotion;
    private List<ShowMotion> list;
    private ShowMotionAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_motion);

        btnBack=findViewById(R.id.btnBack);
        lvShowMotion= (ListView) findViewById(R.id.lvShowMotion);
        motion_id = getIntent().getStringExtra(Config.KEY_MOTION_ID);
        motion_photo_url=getIntent().getStringArrayExtra(Config.KEY_MOTION_PHOTO_URL);
        photo_url=getIntent().getStringExtra(Config.KEY_PHOTO_URL);
        nickname=getIntent().getStringExtra(Config.KEY_NICKNAME);
        date=getIntent().getStringExtra(Config.KEY_DATE);
        motion=getIntent().getStringExtra(Config.KEY_MOTION);

        getHeader();

        final ProgressDialog pd = ProgressDialog.show(ShowMotionActivity.this, getResources().getString(R.string.show_motion_connecting), getResources().getString(R.string.show_motion_connecting_to_server));
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.ACTION, Config.ACTION_SHOW_MOTION);
        params.addBodyParameter(Config.KEY_MOTION_ID, motion_id);
        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                pd.dismiss();

                Gson gson = new Gson();
                list = gson.fromJson(responseInfo.result, new TypeToken<ArrayList<ShowMotion>>() {
                }.getType());

                adapter=new ShowMotionAdapter(ShowMotionActivity.this,list);
                lvShowMotion.setAdapter(adapter);

            }

            @Override
            public void onFailure(HttpException e, String s) {
                pd.dismiss();
                Toast.makeText(ShowMotionActivity.this, R.string.show_motion_fail, Toast.LENGTH_SHORT).show();
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void getHeader(){
        ImageLoader imageLoader=ImageLoader.getInstance();
        LayoutInflater layoutInflater=getLayoutInflater();
        View header = layoutInflater.inflate(R.layout.layout_pals_motion, null);
        View title=header.findViewById(R.id.palsMotionTitle);
        View gvMotionImage = header.findViewById(R.id.gvMotionImage);

        ImageView ivTitleImage = (ImageView) title.findViewById(R.id.ivTitleImage);
        TextView tvTitleNickname = (TextView) title.findViewById(R.id.tvTitleNickname);
        TextView tvTitleDate = (TextView) title.findViewById(R.id.tvTitleDate);
        TextView tvMotion = (TextView) header.findViewById(R.id.tvMotion);
        GridView gvImage = (GridView) gvMotionImage.findViewById(R.id.gvImage);
        ImageView ivImage = (ImageView) gvMotionImage.findViewById(R.id.ivImage);

        imageLoader.displayImage(photo_url, ivTitleImage);
        tvTitleNickname.setText(nickname);
        tvTitleDate.setText(date);
        tvMotion.setText(motion);


        if (motion_photo_url != null && motion_photo_url.length > 1) {
            gvImage.setVisibility(View.VISIBLE);
            ivImage.setVisibility(View.GONE);

            GridViewMotionPhotoAdapter gridViewMotionPhoto = new GridViewMotionPhotoAdapter(motion_photo_url, ShowMotionActivity.this);
            gvImage.setAdapter(gridViewMotionPhoto);

            gvImage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(ShowMotionActivity.this, ImageBrowserActivity.class);
                    intent.putExtra("motionPhotoUrl", motion_photo_url);
                    intent.putExtra("position", position);
                    startActivity(intent);
                }
            });

        } else if (motion_photo_url!= null && motion_photo_url.length == 1) {
            gvImage.setVisibility(View.GONE);
            ivImage.setVisibility(View.VISIBLE);

            imageLoader.displayImage(motion_photo_url[0], ivImage);

            ivImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(ShowMotionActivity.this,ImageBrowserActivity.class);
                    intent.putExtra("motionPhotoUrl",motion_photo_url);
                    startActivity(intent);
                }
            });

        } else if (motion_photo_url == null) {
            gvImage.setVisibility(View.GONE);
            ivImage.setVisibility(View.GONE);
        }

        lvShowMotion.addHeaderView(header);
    }
}
